#### # data_pnas = import(here("Data","data_pnas.rds")) ####

# Libraries
# library(tidyverse)
# library(rio)
# library(here)
# library(srvyr)
# library(kableExtra)

# data_pnas = import(here("Data","data_pnas.rds"))

data_survey = data_pnas |> 
  # Demographics
  mutate(
    PID = pid,
    Race = case_when(
      race == 1 ~ "White",
      race %in% 2:9 ~ "Non-white",
      T ~ NA_character_),
    Education = case_when(
      educ %in% 1:4 ~ "No College",
      educ %in% 5:6 ~ "College"),
    Urban = recode_factor(
      urbanicity2,
      `1` = "Big city",
      `2` = "Smaller city",
      `3` = "Suburban",
      `4` = "Small town",
      `5` = "Rural",
      .default = NULL),
    Gender = ifelse(gender == 1, "Male", "Female"),
    age = year(date) - birthyr,
    Age = case_when(
      age < 35 ~ "18-34",
      age %in% 35:64 ~ "35-64",
      age > 64 ~ "65+"
    ),
    faminc = ifelse(faminc_new == 97, NA, faminc_new),
    Income = factor(case_when(
      faminc %in% 1:3 ~ "Less than $30K",
      faminc %in% 4:6 ~ "$30-$60K",
      faminc %in% 7:9 ~ "$60-$100K",
      faminc %in% 10:12 ~ "$100-$200K",
      faminc > 12 ~ ">$200K",
      .default = NA_character_
    ), levels = c("Less than $30K","$30-$60K","$60-$100K","$100-$200K",">$200K"))
  ) |> 
  # Attitudes
  mutate(
    Strength = case_when(
      pid7 %in% c(1,7) ~ "Strong Partisan",
      pid7 %in% 2:6 ~ "Weak/Lean/Independent"
    ),
    poli_interest = case_when(
      newsint %in% 1:4 ~ abs(newsint-4),
      T ~ NA_integer_
    ),
    Political_Interest = recode_factor(
      poli_interest,
      `0` = "Hardly at all",
      `1` = "Only now and then",
      `2` = "Some of the time",
      `3` = "Most of the time",
      .default = NULL
    ),
    weight = weight * wt_adj # use non-panel-corrected weights for full sample
  ) |> 
  select(PID, Race, Education, bornagain, Urban, Gender, Age, Income,
         Strength, Political_Interest, weight)

svy_vars = names(data_survey)[-length(names(data_survey))]

data_survey_sum = map_dfr(svy_vars, \(x){
  data_survey |> 
    as_survey_design(weights = weight) |> 
    select(!!sym(x)) |> 
    group_by(!!sym(x)) |> 
    summarise(var_prop = survey_prop(vartype = "se"),
              n = unweighted(n())) |> 
    rename_with(.cols = 1, ~ "var_level") |> 
    mutate(var = x, .before = var_level)
}) |> 
  replace_na(list(var_level = "No Response")) |> 
  mutate(
    var = case_when(
      var == 'bornagain' ~ "Evangelical Status",
      var == 'Political_Interest' ~ "Follows Political News",
      .default = var),
    var_prop = round(var_prop*100,2),
    var_prop_se = round(var_prop_se*100,2)
  ) 

tab_s4 = data_survey_sum |>
  mutate(var_prop_c = paste0(var_prop,"% (",var_prop_se,")")) |> 
  select(var_level, n, var_prop_c) |> 
  kable(col.names = c("","Unweighted N", "Survey Weighted Proportion (SE)"),
        format = 'latex',
        booktabs = T,
        longtable = T) |> 
  pack_rows(index = table(fct_inorder(data_survey_sum$var)))

rm(data_survey, data_survey_sum)

tab_s4 |> 
  save_kable(file = here("Tables","Supplementary","table_s4.tex"))